from pymongo import MongoClient

from conf.settings import CLOUDMONITOR_MONGODB, CLOUDMONITOR_DB


db = MongoClient(CLOUDMONITOR_MONGODB)[CLOUDMONITOR_DB]


def main():
    username_list = ['wush2-2@knownsec.com']
    for user in db.users.find({'username': {'$in': username_list}}):
        ouid = user.get('_id')
        phone_v = user.get('phoneVerified', False)
        email_v = user.get('emailVerified', False)
        email = user.get('email', '')
        phone = user.get('phone', '')
        wechat = user.get('wechat', {})

        if not (default := db.alertreceivers.find_one({'uid': ouid, 'isDefault': True})):
            inserted_id = db.alertreceivers.insert_one({
                "phoneVerified": phone_v,
                "emailVerified": email_v,
                "enable": True,
                "isDefault": True,
                "name": "默认告警人",
                "uid": ouid,
                "email": email,
                "phone": phone
            }).inserted_id
        else:
            inserted_id = default.get('_id')
        if not db.alertgroups.find_one({'uid': ouid, 'isDefault': True}):
            db.alertgroups.insert_one({
                "isDefault": True,
                "uid": ouid,
                "enable": True,
                "includeWechat": True if wechat else False,
                "name": "默认组",
                "receiverIds": [
                    inserted_id
                ]
            })


if __name__ == '__main__':
    main()
